package com.epfresh.views.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.adapter.MsgTagAdapter;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.GoodsDetail;
import com.epfresh.bean.ListGoods;
import com.epfresh.bean.MsgTag;
import com.epfresh.api.widget.keyboard.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.epfresh.utils.FormatUtil;
import com.epfresh.api.utils.CashierInputFilter;

import android.text.InputFilter;
import android.widget.Toast;

import com.epfresh.api.utils.PointerInputFilter;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 2017/11/13
 * @Description
 */
public class OfferNumberDialog extends Dialog implements TextView.OnEditorActionListener {
    Activity mActivity;

    public interface OnCommitListener {
        void onCommit(String msg, ListGoods goodsId);
    }

    //private TextView tvSure;
    private EditText etContent;
    //private TextView tvHint;
    private ImageView ivClose;

    private OnCommitListener onCommitListener;

    private String mGoodsId;
    boolean keyBoardShown;

    public OnCommitListener getOnCommitListener() {
        return onCommitListener;
    }

    public void setOnCommitListener(OnCommitListener onCommitListener) {
        this.onCommitListener = onCommitListener;
    }

    public OfferNumberDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        mActivity = (Activity) context;

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_offer_number);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        dialogWindow.setAttributes(lp);
        initView();

    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (onCommitListener != null) {
            onCommitListener.onCommit(etContent.getText().toString(), cartGood);
            dismiss();
        }
        return true;
    }

    KeyboardUtil keyboardUtil;

    private void initView() {
        etContent = (EditText) findViewById(R.id.ed_price);
        etContent.setOnEditorActionListener(this);
        //tvHint = (TextView) findViewById(R.id.tv_number_top);
        //tvSure = (TextView) findViewById(R.id.tv_submit);
        ivClose = (ImageView) findViewById(R.id.iv_close);

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        PointerInputFilter cashierInputFilter = new PointerInputFilter();
        cashierInputFilter.MAX_VALUE = 9999999.99d;
        etContent.setFilters(new InputFilter[]{cashierInputFilter});

        keyboardUtil = new KeyboardUtil(mActivity);
        keyboardUtil.showKeyboard();
        keyboardUtil.attachTo(etContent);

        keyboardUtil.setOnCancelClick(new KeyboardUtil.onCancelClick() {
            @Override
            public void onCancellClick() {
                dismiss();
            }
        });
        keyboardUtil.setOnOkClick(new KeyboardUtil.OnOkClick() {
            @Override
            public void onOkClick() {
                String s = etContent.getText().toString();
                double d = 0;
                try {
                    if (s == null || "".equals(s)) {
                        Toast.makeText(getContext(), "输入有误，请重试", Toast.LENGTH_SHORT).show();

                    } else {
                        d = Double.valueOf(s);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "输入有误，请重试", Toast.LENGTH_SHORT).show();
                }
                onCommitListener.onCommit(etContent.getText().toString(), cartGood);
            }
        });

    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    ListGoods cartGood;

    public void show(final ListGoods cartGood) {
        this.cartGood = cartGood;
        if (cartGood != null) {
            show();
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    InputMethodManager inputManager =
                            (InputMethodManager) etContent.getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(etContent, 0);
                }
            }, 200);
        }

    }


    @Override
    //按键回调方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //判断按键键值做出相应操作
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            dismiss();
            return true;
        }
        //其他按键继承系统属性
        return super.onKeyDown(keyCode, event);
    }


}
